package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.codingyz.cache.aspect.annotation.Cacheable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class itemApiController {

    @Autowired
    private ItemService itemService;

    // 专辑的上架 手动上架
    @PostMapping("/albumOnSale/{albumId}")
    @Operation(summary = "专辑上架")
    public Result albumOnSale(@PathVariable Long albumId) {
        itemService.albumOnSale(albumId);
        return Result.ok();
    }

    // 专辑的下架 手动下架
    @DeleteMapping("/albumOffSale/{albumId}")
    @Operation(summary = "专辑下架")
    public Result albumOffSale(@PathVariable Long albumId) {
        itemService.albumOffSale(albumId);
        return Result.ok();
    }

    // 专辑的批量上架
    @PostMapping("/batchAlbumOnSale")
    @Operation(summary = "批量专辑上架")
    public Result batchAlbumOnSale() {
        for (int i = 1; i < 1598; i++) {
            itemService.albumOnSale((long) i);
        }
        return Result.ok();
    }

    // 专辑的批量下架
    @DeleteMapping("/batchAlbumOffSale")
    @Operation(summary = "专辑下架")
    public Result batchAlbumOffSale() {
        itemService.batchAlbumOffSale();
        return Result.ok();
    }

    // http://localhost:8500/api/search/albumInfo/channel/2
    @GetMapping("channel/{category1Id}")
    @Operation(summary = "根据一级分类id首页信息")
    public Result getChannel(@PathVariable Long category1Id) {
        // 未来会将7个map封装到list中
        // 每个map的一个key是一个三级分类对象 另一个key是该分类下的热度值比较高的6张专辑（list）
        List<Map<String, Object>> mapList = itemService.getChannel(category1Id);
        return Result.ok(mapList);
    }

    // Request URL: http://localhost:8500/api/search/albumInfo
    @PostMapping
    @Operation(summary = "条件搜索")
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery) {
        AlbumSearchResponseVo albumSearchResponseVo = itemService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }

    // http://localhost:8500/api/search/albumInfo/completeSuggest/1
    @GetMapping("completeSuggest/{keyword}")
    @Operation(summary = "自动补全")
    public Result completeSuggest(@PathVariable String keyword) {
        Set<String> suggestSet = itemService.completeSuggest(keyword);
        return Result.ok(suggestSet);
    }

    // Request URL: http://localhost:8500/api/search/albumInfo/751
    @GetMapping("/{albumId}")
    @Operation(summary = "根据专辑id查询该专辑的信息")
    @Cacheable(
            cacheKey = RedisConstant.CACHE_INFO_PREFIX + "#{#args[0]}",
            lockKey = RedisConstant.CACHE_LOCK_SUFFIX + "#{#args[0]}",
            bloomKey = "#{#args[0]}",
            enableBloomFilter = true,
            enableLock = true
    )
    public Result getAlbumInfo(@PathVariable Long albumId) {
        Map<String, Object> result = itemService.getAlbumInfo(albumId);
        return Result.ok(result);
    }

    @PostMapping("preCacheRanking")
    @Operation(summary = "提前将ES中的排行榜数据缓存到Redis")
    public Result preCacheRanking() {
        itemService.preCacheRanking();
        return Result.ok();
    }

    // Request URL: http://localhost:8500/api/search/albumInfo/findRankingList/1/buyStatNum
    @GetMapping("findRankingList/{c1Id}/{byType}")
    @Operation(summary = "根据一级分类id和不同维度查询专辑信息排行榜")
    public Result findRankingList(
            @PathVariable Long c1Id,
            @PathVariable String byType
    ) {
        List<AlbumInfoIndexVo> albumInfoIndexVos = itemService.findRankingList(c1Id, byType);
        return Result.ok(albumInfoIndexVos);
    }

}

